# coding=utf-8
from webapp.config import Config
import os
from jproperties import Properties
import tools.globalvar as globalvar

def setMasterObjectByKey(data, key):
    if data:
        # 获取路径
        filename = Config.ROOT_PATH + os.sep + r"initconfig" + os.sep + "master.properties"
        print(filename)
        p = Properties()
        file_dir = os.path.split(filename)[0]
        store_object = {}
        if not os.path.isdir(file_dir):
            os.makedirs(file_dir)
        # 已存在文件
        if os.path.exists(filename):
            props = p.load(filename)
            props.__setitem__(key, Properties.escape(str(data)))
            # 全部遍历下
            for prop in props:
                props.__setitem__(prop, Properties.escape(props[prop]))
                store_object[prop] = str(props[prop])
            # 保存文件
            props.save(filename)
        else:
            p.__setitem__(key, Properties.escape(str(data)))
            store_object[key] = str(data)
            # 保存文件
            p.save(filename)
